const myAlert = document.querySelector('.alert')
function alertFn(msg, isSuccess) {
  myAlert.classList.add('show')
  myAlert.innerText = msg
  const bgStyle = isSuccess ? 'alert-success' : 'alert-danger'
  myAlert.classList.add(bgStyle)

  setTimeout(() => {
    myAlert.classList.remove('show')
    myAlert.classList.remove(bgStyle)
  }, 2000)
}



document.querySelector('.btn').addEventListener('click', () => {

    const username = document.querySelector('.username').value
    const password = document.querySelector('.password').value
    // console.log(username, password)
    if (username.length < 8) {
        alertFn('用户名必须大于等于8位', false)
      console.log('用户名必须大于等于8位')
      return 
    }
    if (password.length < 6) {
        alertFn('密码必须大于等于6位', false)

      console.log('密码必须大于等于6位')
      return 
    }
  
    axios({
      url: 'http://hmajax.itheima.net/api/login',
      method: 'POST',
      data: {
        username,
        password
      }
    }).then(result => {
      alertFn(result.data.message, true)
      location.href="index.html"
      console.log(result)
      console.log(result.data.message)
    }).catch(error => {
      alertFn(error.response.data.message, false)
      console.log(error)
      console.log(error.response.data.message)
    })
  })